﻿using B.Auth.APi.Application.Command.Role;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Role
{
    public class AddRoleCommandHand : IRequestHandler<AddRoleCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<RoleModel> roledb;

        public AddRoleCommandHand(IBaseRepository<RoleModel> roledb)
        {
            this.roledb = roledb;
        }

        public Task<ApiResult<int>> Handle(AddRoleCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();

            var user = new RoleModel();
            user.RoleName = request.RoleName;
            user.CreateUserName = "张三";
            user.CreateTime = DateTime.Now;
            user.UpdateTime = null;
            user.UodateUserName = "";

          

            user.Del = 0;

            var useshow = roledb.GetAll().Any(x =>x.RoleName==request.RoleName&&x.Del==0);
            if (useshow)
            {

                result.Message = "角色不能重复";
                result.Code = ApiEnum.fail;
                return Task.FromResult(result);

            }
            roledb.Add(user);
            result.Message = "添加成功";
            result.Code = ApiEnum.ok;


            return Task.FromResult(result);
        }
    }
}
